<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\manystore\model\ManystoreShop;
use Exception;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

//渠道
class ManyStore extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //信息
    public function info()
    {
        $this->success('成功');
    }

    //渠道申请
    public function apply()
    {
        $model = ManystoreShop::get(['user_id' => $this->auth->id]);
        $data = $this->request->post();
        $rule = [
            'name' => 'require',
            'address_city' => 'require',
            'address' => 'require',
        ];
        $msg = [
            'name.require' => '请输入渠道名称',
            'address_city.require' => '请选择城市',
            'address.require' => '请选择位置',
        ];

        $validate = new \think\Validate($rule, $msg);

        $res = $validate->check($data);
        if ($res === false) {
            $this->error($validate->getError());
        }

        Db::startTrans();
        try {
            $data['user_id'] = $this->auth->id;
            if($model) {
                $model->save($data);
            }else {
                ManystoreShop::create($data);
            }
            Db::commit();
        }catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('申请成功');
    }

    //申请状态
    public function getstate()
    {
        $info = ManystoreShop::get(['user_id' => $this->auth->id]);
        $this->success('成功', $info);
    }
}